package com.zzit.vjv.controller.documentReview_controller.developer;

import com.zzit.vjv.biz.documentReview_biz.developer.DevAddBiz;
import com.zzit.vjv.biz.documentReview_biz.developer.DevUpdBiz;
import com.zzit.vjv.entity.documentReview_entity.AppInfo;
import com.zzit.vjv.entity.documentReview_entity.AppVersion;
import com.zzit.vjv.entity.documentReview_entity.DevUser;
import com.zzit.vjv.util.FileUtil;
import com.zzit.vjv.util.FilesUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台处理添加控制器
 */
@Controller("devAddController")
@RequestMapping("Add")
public class DevAddController {


    @Resource(name = "devAddBiz")
    private DevAddBiz devAddBiz;

    @Resource(name = "devUpdBiz")
    private DevUpdBiz devUpdBiz;


    /**
     * 添加前台app信息方法
     *
     * @return
     */
    @RequestMapping("/addAppInfo")
    public String addAppInfo(@ModelAttribute AppInfo appInfo,
                             @RequestParam MultipartFile a_logoPicPath,
                             HttpServletRequest request) throws IOException {


        System.out.println("请求值:" + appInfo.toString());

        //上传文件(LOGO)
        Map map = FileUtil.numerousFile(a_logoPicPath, request, "statics");


        //向sql传入参数
        Map<String, Object> map1 = new HashMap<>();

        map1.put("softwareName", appInfo.getSoftwareName());
        map1.put("APKName", appInfo.getAPKName());
        map1.put("supportROM", appInfo.getSupportROM());
        map1.put("interfaceLanguage", appInfo.getInterfaceLanguage());
        map1.put("softwareSize", appInfo.getSoftwareSize());
        map1.put("downloads", appInfo.getDownloads());
        map1.put("flatformId", appInfo.getFlatformId());
        map1.put("categoryLevel1", appInfo.getCategoryLevel1());
        map1.put("categoryLevel2", appInfo.getCategoryLevel2());
        map1.put("categoryLevel3", appInfo.getCategoryLevel3());
        map1.put("appInfo", appInfo.getAppInfo());
        map1.put("devId", appInfo.getId());
        map1.put("createdBy", appInfo.getId());

        map1.put("logoPicPath", map.get("xd").toString());
        map1.put("logoLocPath", map.get("jd").toString());


        System.out.println("相对" + map.get("xd"));
        System.out.println("绝对" + map.get("jd"));

        //调用新增方法
        int row = devAddBiz.addAppInfo(map1);

        System.out.println("成功?" + row);


        return "redirect:/dev/devList";
    }


    @RequestMapping("/addAppVersion")
    public String addAppVersion(@ModelAttribute AppVersion appVersion
                                ,@RequestParam MultipartFile a_downloadLink,
                                HttpServletRequest request,HttpSession httpSession) throws IOException {


        //获取相对,以及绝对路径
        Map<String,Object> fileMap=FileUtil.numerousFile(a_downloadLink,request,"statics");

        //用于向sql传值
        Map<String,Object> map=new HashMap<>();

        //用于拿到用户id
        DevUser devUser = (DevUser) httpSession.getAttribute("devUserSession");

        map.put("appId",appVersion.getAppId());
        map.put("versionNo",appVersion.getVersionNo());
        map.put("versionInfo",appVersion.getVersionInfo());
        map.put("publishStatus",appVersion.getPublishStatus());
        map.put("downloadLink",fileMap.get("xd").toString());
        map.put("versionSize",appVersion.getVersionSize());
        map.put("createdBy",devUser.getId());
        map.put("apkLocPath",fileMap.get("jd").toString());
        map.put("apkFileName",appVersion.getVersionNo()+".apk");



        System.out.println("apk路径"+fileMap.get("xd"));

        System.out.println("对象"+appVersion);

        //调用新增版本方法
        int row=devAddBiz.addAppVersion(map);

        //当添加成功时,进行调用修改最新app版本id
        if(row>0){

            devUpdBiz.updAppInfoVersionIdById( Integer.valueOf(map.get("id").toString()),Integer.valueOf(map.get("appId").toString()));

        }



        System.out.println("返回主键:"+appVersion.getId()+"/或者"+map.get("id"));



        return "redirect:/dev/devList";
    }


}
